package my.kwantlen.assignment2;



import org.apache.commons.net.ftp.*;

import my.kwantlen.assignment2.*;
import android.os.Bundle;
import android.provider.Browser;
import android.widget.Button;
import android.content.Intent;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.ListActivity;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.content.Context;

public class ArrayAd extends ListActivity{
    String[] files;
    String product;
    
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            files = Assignment2finalActivity.ftpGetCurrentWorkingDirectory();
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.listview, R.id.label, files));                      
            
            ListView lv = getListView();
    lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) { 
            	product = ((TextView) view).getText().toString();
              
              if (product.contains(".")){
                      //launchtheApplication
            	  
            	 
            	  
            
            	  try {
            		 Assignment2finalActivity.mFTPClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
            		 Assignment2finalActivity.ftpDownload(Assignment2finalActivity.mFTPClient.printWorkingDirectory() + product,"/sdcard/" + product);
            		 Context context = getApplicationContext();
            		 int duration = Toast.LENGTH_SHORT;

            		 Toast toast = Toast.makeText(context, product + " has been downloaded", duration);
            		 toast.show();
            	  } catch (Exception e) {	
						e.printStackTrace();
					}
            	 
              }
              else{                   
                      //files = Assignment2finalActivity.ftpGetCurrentWorkingDirectory();  - Trying access what is in folder
                      //this.setListAdapter(new ArrayAdapter<String>(this, R.layout.listview, R.id.label, files));
                      Intent i = new Intent(getApplicationContext(), ArrayAd.class);
              // sending data to new activity
              i.putExtra("product", product);
              startActivity(i);
                      files = Assignment2finalActivity.changeWorkingDirectory(product);                       
                      }                              
      }
          
            public String getProduct() {
            
            return product;
                    }               
    });     
    
   }
    
    @Override
    public void onBackPressed() {
    	
    	Intent i = new Intent(getApplicationContext(), ArrayAd.class);
        // sending data to new activity
        i.putExtra("product", product);
        startActivity(i);
    		 files = Assignment2finalActivity.goUpDirectory();
   	 
    	
    }
    
    
    
    }